package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopingCartMapper;
import com.sky.service.ShopingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ：lijing（274312059@qq.com）
 * @date ：Created in 2024-08-04 13:39
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class ShopingCartServiceImpl implements ShopingService {
    @Autowired
    private ShopingCartMapper shopingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前菜品或套餐是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        //对象的属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shopingCartMapper.list(shoppingCart);
        // 如果已经存在，则更新数量，否则添加到购物车
        if (list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shopingCartMapper.updateNumberById(cart);
        }else {
            // 如果不存在，则添加到购物车
            //判断本次添加购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加购物车的是菜品

                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            } else {
                //本次添加购物车的是套餐

                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            shopingCartMapper.insert(shoppingCart);
        }






    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前用户的id
        BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shopingCartMapper.list(shoppingCart);

        return list;
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shopingCartMapper.deleteByUserId(userId);

    }
}